package com.tstudy.laoshibang.camera;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.widget.RelativeLayout;

/**
 * 拍照
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年4月9日 下午5:54:42
 */
public class CameraActivity extends BaseActivity {
	private RelativeLayout mRootLayout;
	private boolean mIsPortrait;
	private boolean mIsCamera;
	Handler mHandler;
	int bottomHeight = (int)BaseApplication.mContext.getResources().getDimension(R.dimen.camera_bottom_height);
	/**
	 * @param isPortrait 是否是头像
	 * @param isCamera 拍照：true，相册：false
	 */
	public static void show(boolean isPortrait,boolean isCamera) {
		Intent intent = new Intent(BaseApplication.mCurrentActivity,CameraActivity.class);
		intent.putExtra(CONSTANT.ARGS.IS_PORTRAIT, isPortrait);
		intent.putExtra(CONSTANT.ARGS.IS_CAMERA, isCamera);
		BaseApplication.mCurrentActivity.startActivity(intent);
		BaseApplication.mCurrentActivity.overridePendingTransition(R.anim.push_in_right,R.anim.push_out_right);  
	}
	
	public static void show(Fragment fragment,int requestCode,boolean isPortrait,boolean isCamera) {
		Intent intent = new Intent(BaseApplication.mCurrentActivity,
				CameraActivity.class);
		intent.putExtra(CONSTANT.ARGS.IS_PORTRAIT, isPortrait);
		intent.putExtra(CONSTANT.ARGS.IS_CAMERA, isCamera);
		fragment.startActivityForResult(intent, requestCode);
//		BaseApplication.mCurrentActivity.overridePendingTransition(R.anim.push_in_right,R.anim.push_out_right);  
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_activity);
		mHandler = new Handler();
		mRootLayout = (RelativeLayout) findViewById(R.id.fragment_activity_root);
		mRootFragmentId = mRootLayout.getId();
		if (mIsCamera) {
			CameraFragment.add(mRootFragmentId, mIsPortrait);
		}else{
			openLibrary();
		}
	}
	/**
	 * 打开相册
	 */
	public void openLibrary() {
		Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
		getImage.addCategory(Intent.CATEGORY_OPENABLE);
		getImage.setType("image/jpeg");
		startActivityForResult(getImage,CONSTANT.REQUEST_CODE_ALBUM);
	}

	@Override
	public void initArgs() {
		mIsPortrait = getIntent().getBooleanExtra(CONSTANT.ARGS.IS_PORTRAIT, false);
		mIsCamera = getIntent().getBooleanExtra(CONSTANT.ARGS.IS_CAMERA, false);
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		CommonUtil.recycleBm(BaseApplication.mCameraBM);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (mFragmentStack.size() > 0) {
				remove(mRootFragmentId,
						mFragmentStack.get(mFragmentStack.size() - 1));
			}
		}
		return false;
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		LogUtil.d("--CameraFragment requestCode==" + requestCode + "--resultCode==" + resultCode);
		super.onActivityResult(requestCode, resultCode, data);
		ContentResolver resolver = BaseApplication.mContext.getContentResolver();
		if (requestCode == CONSTANT.REQUEST_CODE_ALBUM && data != null) {//For camera album image
			try {
				Uri originalUri = data.getData();
				final int orientation = CommonUtil.getAlbumOrientation(originalUri);
				byte[] sAlbumBytes = CommonUtil.readStream(resolver.openInputStream(Uri.parse(originalUri.toString())));
				CommonUtil.recycleBm(BaseApplication.mCameraBM);
				BaseApplication.mCameraBM = CommonUtil.resizeImage(sAlbumBytes,BaseApplication.mScreenWidth,BaseApplication.mScreenHeight-bottomHeight);
				sAlbumBytes = null;
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						CropperFragment.add(mRootFragmentId, mIsPortrait, orientation);
					}
				},CONSTANT.LOAD_DATA_DELAY_TIME);
				
			} catch (Exception e) {
				finish();
			}
		} else if (requestCode != CONSTANT.WEIBO_SSO_REQUEST_CODE ) {
			finish();
		}
	}
}
